package com.meishij.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jash.myutils.utils.ViewUtils;
import com.meishij.R;
import com.meishij.entry.FindCaiDanEntry;
import com.meishij.entry.FindZhuanTiEntry;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 阿飞 on 2016/10/13.
 */

public class FindZhuanTiRecyclerAdapter extends RecyclerView.Adapter<FindZhuanTiRecyclerAdapter.ViewHolder> {

    private Context context;
    private int type = 0;
    public static final int TYPE_HUODONG = 1;
    public static final int TYPE_SHIQU = 2;

    private List<FindZhuanTiEntry.Data> huoDongList;
    private List<FindCaiDanEntry.Data> shiQuList;

    public FindZhuanTiRecyclerAdapter(Context context, List list, @NonNull int type) {
        this.context = context;
        this.type = type;
        switch (type) {
            case TYPE_HUODONG:
                this.huoDongList = list;
                if (list == null) {
                    this.huoDongList = new ArrayList<>();
                }
                break;
            case TYPE_SHIQU:
                this.shiQuList = list;
                if (list == null) {
                    this.shiQuList = new ArrayList<>();
                }
                break;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflateView = LayoutInflater.from(context).inflate(R.layout.activity_find_detail_zhuanti_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflateView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.initView(holder.rootView);

        switch (type) {
            case TYPE_HUODONG:
                FindZhuanTiEntry.Data huodongData = huoDongList.get(position);
                ViewUtils.setTextView(holder.tv_find_detail_zhuanTi_item_title, huodongData.getTitle());
                ViewUtils.setTextView(holder.tv_find_detail_zhuanTi_item_zanNum, huodongData.getZan_num());

                String huoDongPhoto = huodongData.getPhoto();
                if (!TextUtils.isEmpty(huoDongPhoto)) {
                    Uri huoDongPhotoUri = Uri.parse(huoDongPhoto);
                    holder.iv_find_detail_caidan_item_photo.setImageURI(huoDongPhotoUri);
                }

                FindZhuanTiEntry.Data.UserInfo huoDongUserInfo = huodongData.getUser_info();
                if (huoDongUserInfo != null) {
                    String avatar = huoDongUserInfo.getAvatar();
                    if (!TextUtils.isEmpty(avatar)) {
                        Uri avatarUri = Uri.parse(avatar);
                        holder.iv_find_detail_zhuanTi_item_avatar.setImageURI(avatarUri);
                    }
                    String if_v = huoDongUserInfo.getIf_v();
                    int if_vip = Integer.valueOf(if_v);
                    switch (if_vip) {
                        case 0:
                            holder.iv_find_detail_zhuanTi_item_ifV.setVisibility(View.INVISIBLE);
                            break;
                        default:
                            holder.iv_find_detail_zhuanTi_item_ifV.setVisibility(View.VISIBLE);
                            break;
                    }
                }
                break;
            case TYPE_SHIQU:
                FindCaiDanEntry.Data shiQuData = shiQuList.get(position);
                ViewUtils.setTextView(holder.tv_find_detail_zhuanTi_item_title, shiQuData.getTitle());
                ViewUtils.setTextView(holder.tv_find_detail_zhuanTi_item_zanNum, shiQuData.getRecipe_num());

                List<String> photos = shiQuData.getPhotos();
                String shiQuPhoto = photos.get(0);
                if (!TextUtils.isEmpty(shiQuPhoto)) {
                    Uri shiQuPhotoUri = Uri.parse(shiQuPhoto);
                    holder.iv_find_detail_caidan_item_photo.setImageURI(shiQuPhotoUri);
                }

                FindCaiDanEntry.Data.UserInfo shiQuUserInfo = shiQuData.getUser_info();
                if (shiQuUserInfo != null) {
                    String shiQuAatar = shiQuUserInfo.getAvatar();
                    if (!TextUtils.isEmpty(shiQuAatar)) {
                        Uri shiQuAatarUri = Uri.parse(shiQuAatar);
                        holder.iv_find_detail_zhuanTi_item_avatar.setImageURI(shiQuAatarUri);
                    }
                    String if_v = shiQuUserInfo.getIf_v();
                    int if_vip = Integer.valueOf(if_v);
                    switch (if_vip) {
                        case 0:
                            holder.iv_find_detail_zhuanTi_item_ifV.setVisibility(View.INVISIBLE);
                            break;
                        default:
                            holder.iv_find_detail_zhuanTi_item_ifV.setVisibility(View.VISIBLE);
                            break;
                    }
                }
                break;
        }


    }

    public void add(Object item) {
        switch (type) {
            case TYPE_HUODONG:
                huoDongList.add((FindZhuanTiEntry.Data) item);
                break;
            case TYPE_SHIQU:
                shiQuList.add((FindCaiDanEntry.Data) item);
                break;
        }
        notifyDataSetChanged();
    }

    public void addAll(List items) {
        switch (type) {
            case TYPE_HUODONG:
                huoDongList.addAll(items);
                break;
            case TYPE_SHIQU:
                shiQuList.addAll(items);
                break;
        }
        notifyDataSetChanged();
    }

    public void clear() {
        switch (type) {
            case TYPE_HUODONG:
                huoDongList.clear();
                break;
            case TYPE_SHIQU:
                shiQuList.clear();
                break;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        switch (type) {
            case TYPE_HUODONG:
                return huoDongList == null ? 0 : huoDongList.size();
            case TYPE_SHIQU:
                return shiQuList == null ? 0 : shiQuList.size();
            default:
                return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View rootView;

        // huodong
        SimpleDraweeView iv_find_detail_caidan_item_photo;
        SimpleDraweeView iv_find_detail_zhuanTi_item_avatar;
        TextView tv_find_detail_zhuanTi_item_zanNum;
        TextView tv_find_detail_zhuanTi_item_title;
        ImageView iv_find_detail_zhuanTi_item_ifV;

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
        }

        public void initView(View rootView) {
            tv_find_detail_zhuanTi_item_zanNum = (TextView) rootView.findViewById(R.id.tv_find_detail_zhuanTi_item_zanNum);
            tv_find_detail_zhuanTi_item_title = (TextView) rootView.findViewById(R.id.tv_find_detail_zhuanTi_item_title);
            iv_find_detail_caidan_item_photo = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_detail_caidan_item_photo);
            iv_find_detail_zhuanTi_item_avatar = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_detail_zhuanTi_item_avatar);
            iv_find_detail_zhuanTi_item_ifV = (ImageView) rootView.findViewById(R.id.iv_find_detail_zhuanTi_item_ifV);
        }
    }

}
